package com.cugmarket.controller.product.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cugmarket.constant.MessageConstant;
import com.cugmarket.dto.Result;
import com.cugmarket.entity.ProductCollect;
import com.cugmarket.entity.ProductType;
import com.cugmarket.mapper.ProductTypeMapper;
import com.cugmarket.service.ProductTypeService;
import com.cugmarket.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.cugmarket.constant.MessageConstant.*;

@Slf4j
@RestController("adminProductTypeController")
@RequestMapping("/admin/productType")
public class ProductTypeController {
    @Resource
    private ProductTypeMapper productTypeMapper;

    @PostMapping("/add")
    public Result add(@RequestBody ProductType productType) {
        // 查询是否存在相同名称的商品分类
        LambdaQueryWrapper<ProductType> queryWrapper = new LambdaQueryWrapper<ProductType>().eq(ProductType::getTypeName, productType.getTypeName())
                .eq(ProductType::getId, productType.getId());
        long count = productTypeMapper.selectCount(queryWrapper);
        if (count > 0) {
            return Result.error(MessageConstant.REAPET_ERROR);
        }
        int result = productTypeMapper.insert(productType);
        if (result > 0) {
            return Result.success(INSERT_SUCCESS);
        } else {
            return Result.error(INSERT_ERROR);
        }
    }

    @DeleteMapping("/remove")
    public Result del(@RequestParam("id") String id) {
        int result = productTypeMapper.deleteById(id);
        if (result > 0) {
            return Result.success(DELETE_SUCCESS);
        } else {
            return Result.error(DELETE_ERROR);
        }
    }
}
